#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1e6+10;
const int mod = 1e9+7;

int n,m,pnum,prime[N],mu[N];
ll s[N],f[N],invf[N];
bool v[N];

ll qp(ll x,int t){ll res=1;for(;t;t>>=1,x=x*x%mod)if(t&1)res=res*x%mod;return res;}

ll inv(ll x){return qp(x,mod-2);}

void init(){
	f[0] = 0,f[1] = 1;s[0] = s[1] = 1;invf[1] = inv(f[1]), invf[0] = inv(f[0]);
	for(int i = 2;i < N;i++) f[i] = (f[i-1] + f[i-2]) % mod,s[i] = 1,invf[i] = inv(f[i]);
	mu[1] = 1;
	for(int i = 2;i < N;i++){
		if(!v[i]) prime[++pnum] = i,mu[i] = -1;
		for(int j = 1;j <= pnum && prime[j] * i < N;j++){
			v[prime[j] * i] = 1; mu[i * prime[j]] = -mu[i];
			if(i % prime[j] == 0){mu[i * prime[j]] = 0;break;}
		}
	}
	for(int i = 1;i < N;i++)
		if(mu[i] != 0)
			for(int j = i;j < N;j+=i)
				s[j] = s[j] * (mu[i] == 1 ? f[j/i] : invf[j/i]) % mod;
	for(int i = 1;i < N;i++) s[i] = s[i-1] * s[i] % mod;
}

void solve(){
	n = in,m = in; if(n > m) swap(n,m);ll ans = 1;
	for(int l = 1,r;l <= n;l = r+1){
		r = min(n/(n/l),m/(m/l));
		ans = ans * qp(s[r] * inv(s[l-1]) % mod,1ll*(n/l)*(m/l)%(mod-1)) % mod;
	}
	printf("%lld\n",ans);
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	int T = in; init();
	while(T--) solve();
	return 0;
}

